package cn.signalpha.modules.base.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.base.dto.VersionsDTO;
import cn.signalpha.modules.base.entity.Versions;
import cn.signalpha.modules.base.service.VersionsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/versions")
@Api(value = "版本更新", tags = "版本更新")
public class VersionsController extends BaseController {

	private final VersionsService versionsService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<Versions>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<Versions> queryWrapper = Condition.getQueryWrapper(params, Versions.class);
        return R.data(versionsService.page(Condition.getPage(query), queryWrapper.lambda().orderByDesc(Versions::getId)));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Versions> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(versionsService.getById(id));
	}

	@PostMapping("/submit")
	@ApiOperation(value = "提交", notes = "传入versions")
	public R submit(@Valid @RequestBody VersionsDTO versions) {
		versionsService.removeCaChe();
		return R.status(versionsService.saveOrUpdate(BeanUtil.copy(versions, Versions.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		versionsService.removeCaChe();
		return R.status(versionsService.removeByIds(Func.toLongList(ids)));
	}

}
